import store from '@/store';
import router from '@/router';

/**
 * @method
 * @description 提示
 * @param {String} content 要提示的内容
 * @returns 
 */
export function confirm(content) {
    return new Promise((res, rej) => {
        this.$Modal.confirm({
            title: "提示",
            content,
            cancelText: "取消",
            onOk() {
                res();
            },
            onCancel() {
                rej();
            }
        });
    });
}

/**
 * @method
 * @description 处理菜单处理
 * @param {Array} arr 要处理的菜单数据
 * @returns 
 */
export function handleMenu(arr) {
    const newArr = [];
    const items = arr.filter(c => c.ParentId === 0);
    (function deep(_items, _arr) {
        for (let item of _items) {
            item.title = item.Name;
            _arr.push(item);
            const children = arr.filter(c => c.ParentId === item.Id);
            children.length && deep(children, item.children = []);
        }
    })(items, newArr)
    return newArr;
}

/**
 * @method
 * @description 深拷贝
 * @param {Object} arr 要拷贝的数据
 * @returns 
 */
export function deepClone(arr) {
    if (!arr || typeof arr !== 'object') {
        return arr;
    }
    // const _arr = Array.isArray(arr) ? [] : {};
    // +function clone(source, obj) {
    // 	for (const key in source) {
    // 		if (source.hasOwnProperty(key)) {
    // 			const item = source[key];
    // 			if (typeof item === 'object') {
    // 				clone(item, obj[key] = Array.isArray(item) ? [] : {});
    // 			} else {
    // 				obj[key] = item;
    // 			}
    // 		}
    // 	}
    // }(arr, _arr)
    // return _arr;
    return JSON.parse(JSON.stringify(arr));
}

/**
 * @method
 * @description 验证权限
 * @param {String} name 要验证的权限名称
 */
export function validR(name) {
    return store.state.myMenus.some(c => c.Code === name);
}

/**
 * @method
 * @description 验证时间
 * @param {Date} start 开始时间
 * @param {Date} end 结束时间
 */
export function validDate(start, end) {
    if (!start || !end) {
        return true;
    }
    try {
        if (!(start instanceof Date)) {
            start = new Date(start);
        }
        if (!(end instanceof Date)) {
            end = new Date(end);
        }
        start = new Date(start.getFullYear(), start.getMonth(), start.getDate());
        end = new Date(end.getFullYear(), end.getMonth(), end.getDate());
        return start <= end;
    } catch (error) {
    }
    return true;
}

/**
 * @method
 * @description 合并问答列表
 * @param {Array} list 列表
 * @param {String} qKey 问题的键
 * @param {String} aKey 答案的键
 */
export function mergeQAList(list, qKey = 'Question', aKey = 'Answer') {
    const objs = {};
    list.forEach((item) => {
        const parent = objs[item[qKey]] || (objs[item[qKey]] = []);
        parent.push(item[aKey]);
    });
    const lst = [];
    for (const key in objs) {
        const item = objs[key];
        item.forEach((child, index) => {
            lst.push({
                show: index === 0,
                rowspan: index === 0 ? item.length : 0,
                title: key,
                answer: child,
            });
        });
    }
    return lst;
}

/**
 * @method
 * @description 退出
 */
export function exit() {
    store.commit("setUser", {});
    router.push({ name: 'login' });
}

/**
 * @method
 * @description 设置产品信息
 * @param {String} title 标题
 * @param {String} model 型号
 * @param {String} version 版本
 */
function setProduct(title, model, version) {
    store.commit('setProduct', { Title: title, Model: model, Version: version });
}

window.setProduct = setProduct;

export default {
    install(Vue) {
        Vue.prototype.$deepClone = deepClone;
        Vue.prototype.$handleMenu = handleMenu;
        Vue.prototype.$confirm = confirm;
        Vue.prototype.$validR = validR;
        Vue.prototype.$validDate = validDate;
        Vue.prototype.$mergeQAList = mergeQAList;
        Vue.prototype.$exit = exit;

        Vue.directive('can', {
            inserted(el, binding) {
                if (binding.arg && !validR(binding.arg)) {
                    return el.parentNode.removeChild(el);
                }
                const values = binding.value;
                if (Array.isArray(values) && !values.some(c => validR(c))) {
                    el.parentNode.removeChild(el);
                }
            }
        })
    }
}